<template>
	<div class="erp-search-button">
		<el-dropdown @command="changeTime">
			<el-button @click="searchTable" type="primary" size="small" :round="roundFlag" :loading="saveLoading" :icon="Search">搜 索</el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item :command="type.value" v-for="type in timeTypeList" :key="type.value">{{ type.name }}</el-dropdown-item>
        </el-dropdown-menu>
      </template>
		</el-dropdown>
	</div>
</template>

<script setup>
import { getLastMonday, getLastSunday, curStartTime,curEndTime,
  formatDate, GetDateStr, getFirstWeek, getLastWeek, getSatWeek,
  getNextMonday, getNextSunday,getFirstOneDay,
  getLastOneDay, getFirstDayOfQuarter, getLastDayOfQuarter,
  getLastMonthFirstOneDay,getLastMonthEndDay,
  getLastQuarterFirstDay,getLastQuarterLastDay
} from '@/utils/d_times.js'
import { Search } from '@element-plus/icons-vue'
const emits = defineEmits(["searchTable"])
const props = defineProps({
  saveLoading: {
    type: Boolean,
    default: false
  },
  roundFlag: {
    type: Boolean,
    default: false
  },
})
const timeTypeList = [
  {name: '今天', value: 'today'},
  {name: '昨天', value: 'yesterday'},
  {name: '明天', value: 'tomorrow'},
  {name: '本周', value: 'thisWeek'},
  {name: '上周', value: 'lastWeekend'},
  {name: '本月', value: 'thisMonth'},
  {name: '上月', value: 'lastMonth'},
  {name: '本季度', value: 'thisQuarter'},
  {name: '上季度', value: 'lastQuarter'},
]
const searchTable = () => {
  emits("searchTable")
}
const changeTime = (data) => {
  let start = "";
  let end = "";
  switch(data){
    case 'today' :  start = GetDateStr(0); end = GetDateStr(0);
      break;
    case 'yesterday' :  start = GetDateStr(-1); end = GetDateStr(-1);
      break;
    case 'tomorrow' :  start = GetDateStr(1); end = GetDateStr(1);
      break;
    case 'thisWeek' :  start = getFirstWeek(); end = getLastWeek();
      break;
    case 'weekend' :  start = getSatWeek(); end = getLastWeek();
      break;
    case 'lastWeekend' :  start = getLastMonday(); end = getLastSunday();
      break;
    case 'thisMonth' :  start = getFirstOneDay(); end = getLastOneDay();
      break;
    case 'lastMonth' :  start = getLastMonthFirstOneDay(); end = getLastMonthEndDay();
      break;
    case 'thisQuarter' :  start = getFirstDayOfQuarter(); end = getLastDayOfQuarter();
      break;
    case 'lastQuarter' :  start = getLastQuarterFirstDay(); end = getLastQuarterLastDay();
      break;
  }
  start = curStartTime(start);
  end = curEndTime(end);
  emits("searchTable", [start, end])
}
</script>

<style scoped lang="scss">
.erp-search-button {
	margin-left: 10px;
}
</style>
